package bb.lanxing.adapter.holder;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import bb.lanxing.R;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Enums;
import bb.lanxing.util.SpliceBitmapUtil;
import bb.lanxing.util.SportTypeResUtil;
import bb.lanxing.util.WorkoutTitleUtil;
import bb.lanxing.util.image.GlideUtils;

public class WorkoutMergeListViewHolder extends RecyclerView.ViewHolder {
    static DisplayImageOptions mapOptions;
    private View bottomLine;
    private ImageView cadenceView;
    private TextView commentText;
    private TextView distanceText;
    private ImageView historyHideView;
    private ImageView historySyncView;
    private TextView likeText;
    private ImageButton longPressDragView;
    private ImageView mapThumb;
    private TextView scoreText;
    private TextView scoreUnitText;
    private ImageView sportType;
    private TextView titleText;

    public WorkoutMergeListViewHolder(View itemView) {
        super(itemView);
        this.sportType = (ImageView) itemView.findViewById(R.id.item_history_content_sport);
        this.mapThumb = (ImageView) itemView.findViewById(R.id.item_history_content_thumb);
        this.titleText = (TextView) itemView.findViewById(R.id.item_history_content_title);
        this.historySyncView = (ImageView) itemView.findViewById(R.id.historySyncIcon);
        this.distanceText = (TextView) itemView.findViewById(R.id.item_history_content_distance);
        this.scoreText = (TextView) itemView.findViewById(R.id.item_history_content_score);
        this.scoreUnitText = (TextView) itemView.findViewById(R.id.item_history_content_score_unit);
        this.commentText = (TextView) itemView.findViewById(R.id.item_history_content_comment);
        this.likeText = (TextView) itemView.findViewById(R.id.item_history_content_like);
        this.cadenceView = (ImageView) itemView.findViewById(R.id.cadenceTypeIcon);
        this.historyHideView = (ImageView) itemView.findViewById(R.id.historyHideIcon);
        this.longPressDragView = (ImageButton) itemView.findViewById(R.id.item_history_long_press_drag);
        if (mapOptions == null) {
            mapOptions = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.img_map_thumb)
                    .showImageForEmptyUri(R.drawable.img_map_thumb)
                    .showImageOnFail(R.drawable.img_map_thumb)
                    .displayer(new FadeInBitmapDisplayer(300))
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .build();
        }
    }

    public void bind(Workout workout) {
        String str;
        String string;
        int color;
        if ((workout.getSport() == Workout.TYPE_TRAINING)
            || (workout.getSport() == Workout.TYPE_INDOOR_BIKE)
            || (workout.getSport() == Workout.TYPE_BIKE_TRAINER)
            || (workout.getSport() == Workout.TYPE_ROWER)
            || (workout.getSport() == Workout.TYPE_CROSS_TRAINER)){
            this.mapThumb.setImageResource(R.drawable.ic_history_list_thumb_training);
        } else {
            String str2 = workout.getThumbnail() + "!workoutThumb";
            long uploadTime = workout.getUploadTime();
            if (workout.getUploadStatus() != Enums.UploadStatus.Uploaded || System.currentTimeMillis() - uploadTime > 300000) {
                str = str2 + "?upload_time=" + workout.getEndTime() + workout.getUploadStatus().toString();
            } else {
                str = str2 + "?upload_time=" + System.currentTimeMillis() + workout.getUploadStatus().toString();
            }
            GlideUtils.companion.loadImage(this.mapThumb.getContext(), GlideUtils.companion.appendImagePath(str, 13), this.mapThumb, R.drawable.img_map_thumb);
        }
        this.sportType.setImageResource(SportTypeResUtil.getHistoryTypeIcon(workout.getSport()));
        if (TextUtils.isEmpty(workout.getTitle())) {
//            TextView textView = this.titleText;
            titleText.setText(WorkoutTitleUtil.buildWorkoutDefaultTitle(titleText.getContext(), workout));
        } else {
            this.titleText.setText(workout.getTitle());
        }
        TextView textView2 = this.titleText;
        textView2.setTextColor(textView2.getResources().getColor(R.color.history_item_date_text_color));
        if (workout.getUploadStatus() == Enums.UploadStatus.Uploaded) {
            this.historySyncView.setVisibility(View.GONE);
        } else {
            this.historySyncView.setVisibility(View.VISIBLE);
        }
        this.distanceText.setText(CommonUtil.getFormatDistance(workout.getDistance()));
        Context context = this.itemView.getContext();
        if (workout.getServerId() > 0) {
            string = context.getString(R.string.hot_value);
            this.scoreText.setText(String.valueOf(Math.round(workout.getCredits())));
            color = context.getResources().getColor(R.color.global_orange_color);
        } else {
            this.scoreText.setText("");
            string = context.getString(R.string.hot_not_upload);
            color = context.getResources().getColor(R.color.grey_999999);
        }
        this.scoreUnitText.setTextColor(color);
        this.scoreUnitText.setText(string);
        this.commentText.setText(String.valueOf(workout.getCommentCount()));
        this.likeText.setText(String.valueOf(workout.getLikeCount()));
        this.historyHideView.setVisibility(workout.getHidden() == 1 ? View.VISIBLE : View.GONE);
        Drawable generateWorkoutFeatures = SpliceBitmapUtil.generateWorkoutFeatures(context,
                workout.getCadenceSource(), workout.getHeartSource(), workout.getLocSource(),
                workout.hasMatchedSegment(), workout.getSlopeGradeMax(), workout.isThreedWorkout());
        this.cadenceView.setVisibility(generateWorkoutFeatures != null ? View.VISIBLE : View.GONE);
        this.cadenceView.setImageDrawable(generateWorkoutFeatures);
    }

    public View getDragView() {
        return this.longPressDragView;
    }
}
